This page last changed on Jan 11, 2009 by straha1.
Tutorial

This page is part of a tutorial that explains how to

Table of Contents

Simple Hello World

Let's try to compile this simple Fortran 90 program which we'll put in a file helloworld.f90:

program HelloWorldF90
    write(*,*) "Greetings, denizens of planet Earth!"
end program HelloWorldF90

To compile this using GCC and create the executable helloworld-f90-gcc, type

gfortran helloworld.f90 -o helloworld-f90-gcc

Alternatively, to compile using PGI Fortran and create the executable helloworld-f90-pgi, type

pgf90 helloworld.f90 -o helloworld-f90-pgi

Running this program by typing ./helloworld-f90-gcc for GCC or ./helloworld-f90-pgi for PGI should produce this output:

 Greetings, denizens of planet Earth!

Parallel Hello World

Before you try to compile this program, you must set up your MPI and compiler using switcher as described here in the C tutorial. Make sure you follow the directions in that section before continuing.

Once you've used switcher to set up your compiler+MPI combination, create this Fortran 90 program hello_parallel.f90:

program hello_parallel

  ! Include the MPI library definitons:
  include 'mpif.h'

  integer numtasks, rank, ierr, rc, len, i
  character*(MPI_MAX_PROCESSOR_NAME) name

  ! Initialize the MPI library:
  call MPI_INIT(ierr)
  if (ierr .ne. MPI_SUCCESS) then
     print *,'Error starting MPI program. Terminating.'
     call MPI_ABORT(MPI_COMM_WORLD, rc, ierr)
  end if

  ! Get the number of processors this job is using:
  call MPI_COMM_SIZE(MPI_COMM_WORLD, numtasks, ierr)

  ! Get the rank of the processor this thread is running on.  (Each
  ! processor has a unique rank.)
  call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierr)

  ! Get the name of this processor (usually the hostname)
  call MPI_GET_PROCESSOR_NAME(name, len, ierr)
  if (ierr .ne. MPI_SUCCESS) then
     print *,'Error getting processor name. Terminating.'
     call MPI_ABORT(MPI_COMM_WORLD, rc, ierr)
  end if

  print "('hello_parallel.f: Number of tasks=',I3,' My rank=',I3,' My name=',A,'')",&
       numtasks, rank, trim(name)

  ! Tell the MPI library to release all resources it is using:
  call MPI_FINALIZE(ierr)

end program hello_parallel

Now that you've written your hello_parallel program, it is time to compile it with:

mpif90 hello_parallel.f90 -o hello_parallel

That same command will work with any of the six compiler+MPI combinations.

To learn how to run this program, continue on to this page.

If you intend to create more complex programs that use multiple source files, you will need to learn how to link programs. (For example, if you have one source file with your main program and three more files with subroutines needed by the main program.) To learn about linking MPI and non-MPI programs, go to this page.

Document generated by Confluence on Mar 31, 2011 15:37